<template>
	<view class="index_content_basic">
		<view class="index_content_basic_main">
			<u--form :model="ruleForm" :rules="rules" label-position="top" ref="ruleForm" label-width="300px"
				class="demo-ruleForm">
				<u-form-item label="填写说明:" prop="">
					<text class="fontz fonts12 gray">未婚/离异填父母、兄弟姐妹；已婚填配偶、子女</text>
				</u-form-item>
				<u-form-item label="称谓" prop="title">
					<u-input v-model="ruleForm.title" placeholder="请输入称谓"></u-input>
				</u-form-item>
				<u-form-item label="姓名" prop="fullName">
					<u-input v-model="ruleForm.fullName" placeholder="请输入姓名"></u-input>
				</u-form-item>
				<u-form-item label="出生年月" prop="">
					<TimeSelect :placeholders="'出生年月'" :value="ruleForm.brithDay" :keys="'brithDay'" @mySelectclick="mySelectclick"></TimeSelect>
					<!-- <el-date-picker type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss"  placeholder="请选择出生年月" v-model="ruleForm.brithDay" style="width: 100%;">
					</el-date-picker> -->
				</u-form-item>
				<u-form-item label="政治面貌" prop="">
					<!-- <el-select v-model="ruleForm.politicalOutlook" placeholder="请选择政治面貌" style="width: 100%;">
						<el-option v-for="item in politicalOutlookOption" :label="item.dictLabel" :value="item.dictValue">
						</el-option>
					</el-select> -->
					<mySelect :dict="'political_outlook'" :value="ruleForm.politicalOutlook" :keys="'politicalOutlook'" @mySelectclick="mySelectclick"></mySelect>
				</u-form-item>
				<u-form-item label="工作单位及职位" prop="">
					<u-input v-model="ruleForm.workUnitAndPosition" placeholder="请输入工作单位及职位"></u-input>
				</u-form-item>
				<u-form-item label="联系方式" prop="">
					<u-input v-model="ruleForm.contactInformation" maxlength="11" placeholder="请输入联系方式"></u-input>
				</u-form-item>
			</u--form>
		</view>
		<view class="position_footer flex aic jcsb">
			<u-button class="blue_btn" style="width: 100%;" type="primary" @click="submitForm('ruleForm')">保存
			</u-button>
			<view class="" style="width: 44%;margin-left: 16px;" v-if="type == 0">
				<u-button class="" type="error" @click="deleteFun()" >
					删除
				</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		editResume
	} from "@/api/my.js"
	import mySelect from "../../../components/mySelect.vue"
	import TimeSelect from "../../../components/TimeSelectMonth.vue"
	export default {
		components: {
			mySelect,
			TimeSelect
		},
		data() {
			var checkPhone = (rule, value, callback) => {
				if (!value) {
				} else {
					const reg = /^1[3-9][0-9]\d{8}$/;
					// 这里定义的reg变量没有加引号，所以是一种特殊的数据类型，即正则，可以直接用test()方法
					if (reg.test(value)) {
						callback();
					} else {
						// 具体错误信息没有提示
						return callback(new Error("请输入正确的手机号"));
					}
				}
			};
			return {
				ruleForm: {
					title:"",
					fullName:"",
					brithDay:"",
					politicalOutlook:"",
					workUnitAndPosition:"",
					contactInformation:""
					
				},
				rules: {
					title: [{
						required: true,
						message: '请输入称谓',
						trigger: 'blur'
					}],
					fullName: [{
						required: true,
						message: '请输入姓名',
						trigger: 'blur'
					}],
					contactInformation:[{
						validator: checkPhone,
						trigger: "change"
					}]
				},
				indexValue: "",
				type: 0,
				politicalOutlookOption:[]
			};
		},
		onLoad(options) {
			this.politicalOutlookOption= uni.getStorageSync("political_outlook")
			this.datas = JSON.parse(options.data)
			this.type = options.type
			if (options.type == 0) { //修改
				this.indexValue = options.index
				this.info(this.datas, options.index)
			} else { //新增	
			}
		
		},
		onReady() {
			this.$refs.ruleForm.setRules(this.rules);
		},
		methods: {
			mySelectclick(e){
				console.log("下拉框组件回传:",e);
				this.ruleForm[e.key]=e.value
				this.timeFun()
			},
			timeFun(){
				this.ruleForm.brithDay=this.ruleForm.brithDay.slice(0,7)
			},	
			timeFunEnd(){
				this.ruleForm.brithDay=this.ruleForm.brithDay+"-01 00:00:00"
			},
			info(data, index) {
				console.log("data:", data);
				this.ruleForm = data.familyMembers[index]
				this.timeFun()
			},
			deleteFun(){
				let obj = this.datas
				obj.familyMembers.splice(this.indexValue,1)
				editResume(obj).then(res => {
					uni.showToast({
						title: "已删除",
						icon: 'error'
					})
					if (this.type == 1) {
						uni.navigateBack("-1")
					}
				}).catch(res => {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					})
				})
			},
			submitForm(formName) {
				this.$refs.ruleForm.validate().then(valid => {
					if (valid) {
						let obj = this.datas
						console.log("数据：",obj);
						this.timeFunEnd()
						if (this.type == 0) {
							obj.familyMembers[this.indexValue] = this.ruleForm
						} else {
							obj.familyMembers.push(this.ruleForm)
						}
			
						editResume(obj).then(res => {
							uni.showToast({
								title: "保存成功",
								icon: 'success'
							})
							if (this.type == 1) {
								uni.navigateBack("-1")
							}
						}).catch(res => {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							})
						})
					} else {
						uni.showToast({
							title: "请完善信息",
							icon: 'error'
						})
						console.log('error submit!!');
						return false;
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.blue_btn {
		height: 40px;
		background: #2F6CBC;
		border: 1px solid #2F6CBC;
	}
</style>
